package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnImageView;
import ar.com.mooral.data.Depositions;
import ar.com.mooral.data.FoodList;
import ar.com.mooral.data.SexualActivityIncidence;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.FoodListPojo;
import ar.com.mooral.pojo.util.PojoComparatorDate;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class AlimentacionComidasRecientes extends CrohnActivity {
	
    
	protected void onStart() {
		super.onStart();

        FoodList foodList = new FoodList(this);
        Vector<Hashtable<String, String>> data = foodList.findAll();
        drawGrid(data);
        
        
        // NUEVA COMIDA RECIENTE
        Button btnComidasRecientesAgregarComidaReciente = (Button) findViewById(R.id.btnComidasRecientesAgregarComidaReciente);
        btnComidasRecientesAgregarComidaReciente.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	AlimentacionComidasRecientesNuevaComida.class.getPackage().getName(), 
														AlimentacionComidasRecientesNuevaComida.class.getName()));
				
         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
         		parentActivity.startChildActivity("AlimentacionComidasRecientesNuevaComida", intent);
				
				//startActivity(intent);
		    }
		});
	        

        /* NUEVA COMIDA RECIENTE
        ImageButton btnComidasRecientesNuevaComida = (ImageButton) findViewById(R.id.btnComidasRecientesNuevaComida);
        btnComidasRecientesNuevaComida.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	AlimentacionComidasRecientesNuevaComida.class.getPackage().getName(), 
														AlimentacionComidasRecientesNuevaComida.class.getName()));
				startActivity(intent);
		    }
		});
		*/
	}

    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alimentacion_comidas_recientes);

        //RETORNAR
    	Button btnAlimentacionComidasRecientesRetorno = (Button) findViewById(R.id.btnAlimentacionComidasRecientesRetorno);
    	btnAlimentacionComidasRecientesRetorno.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});
        
    }
    
    
    private void drawGrid(Vector data) {
    	
        LinearLayout records = (LinearLayout)findViewById(R.id.linearLayoutAlimentacionComidasRecientes);
        
        records.removeAllViews();
        
        Collections.sort((Vector<BasePojo>)data, new PojoComparatorName());
        
        for (int i = 0; i < data.size(); i++) {

        	
        	BasePojo record = (BasePojo) data.get(i);

        	
        	/*
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setPadding(5, 5, 5, 5);
			ll.setGravity(Gravity.CENTER_VERTICAL);
			records.addView(ll);
			*/
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
			ll.setPadding(10, 0, 0, 0);
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setBackgroundResource(R.drawable.medication_bg);
			ll.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
			records.addView(ll);			
        	

		
			CrohnImageView deleteImageView = new CrohnImageView(this);
			deleteImageView.setBackgroundResource(R.drawable.stop);
			deleteImageView.setLayoutParams(new LayoutParams(20, 20));
			deleteImageView.setPojo(record);
			deleteImageView.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnImageView) view).getPojo();
    		    	basePojo.getDatabaseHelper().delete(basePojo.getId());
    		    	((LinearLayout) view.getParent().getParent()).removeView((View) view.getParent());
    		    }
            });
			ll.addView(deleteImageView);

			
			CrohnButton recordButton = new CrohnButton(this);
			String text = record.getName();
			if(text.length()>30) text = text.substring(0,30) + "...";
			recordButton.setText(text);
			recordButton.setBackgroundResource(R.drawable.white);
			recordButton.setLayoutParams(new LayoutParams(280, 30));
			//recordButton.setTextColor(Color.BLACK);
			recordButton.setTextSize(10);
			recordButton.setPojo(record);
			recordButton.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	
    		    	BasePojo basePojo = ((CrohnButton) view).getPojo();
    		    	CrohnContext.setFoodName(basePojo.getName());
    		    	
					finish();
    		    }
            });
			ll.addView(recordButton);
			
		    	
        } // for
    	
    }
    
}
